﻿using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using DTO;

namespace DAO
{
    public class LoaiPhongDAO:DataProvider
    {
        public List<LoaiPhongDTO> GetAll()
        {
            SqlConnection connection = MoKetNoi();
            string sql = "SELECT * FROM LoaiPhong";
            SqlCommand command = new SqlCommand(sql, connection);
            SqlDataReader reader = command.ExecuteReader();
            List<LoaiPhongDTO> dskh = new List<LoaiPhongDTO>();
            LoaiPhongDTO pb;
            while (reader.Read())
            {
                pb = new LoaiPhongDTO();
                pb.Maloaiphong = reader.GetString(0);
                pb.Tenloaiphong = reader.GetString(1);
                pb.Gia = double.Parse(reader.GetDecimal(2).ToString());
                pb.Soktd = reader.GetInt32(3);
                dskh.Add(pb);
            }
            DongKetNoi(connection);
            return dskh;
        }
        public bool Insert(LoaiPhongDTO lp)
        {
            bool kq = true;
            SqlConnection connection = MoKetNoi();
            string sql = "insert into LoaiPhong(Ma_LoaiPhong,TenLoaiPhong,Gia,SoKhachToiDa) values('" + lp.Maloaiphong + "',N'" + lp.Tenloaiphong + "','" + lp.Gia + "','" + lp.Soktd + "')";
            SqlCommand command = new SqlCommand(sql, connection);
            int x = command.ExecuteNonQuery();
            if (x <= 0)
                kq = false;
            DongKetNoi(connection);
            return kq;
        }
        public bool Update(LoaiPhongDTO lp)
        {
            bool kq = true;
            SqlConnection connection = MoKetNoi();
            string sql = "update LoaiPhong set TenLoaiPhong = (N'" + lp.Tenloaiphong + "'),Gia = ('" + lp.Gia + "'),SoKhachToiDa = ('" + lp.Soktd + "') where Ma_LoaiPhong = ('" + lp.Maloaiphong + "')";
            SqlCommand command = new SqlCommand(sql, connection);
            int x = command.ExecuteNonQuery();
            if (x <= 0)
                kq = false;
            DongKetNoi(connection);
            return kq;
        }
        public bool Delete(LoaiPhongDTO lp)
        {
            bool kq = true;
            SqlConnection connection = MoKetNoi();
            string sql = "delete from LoaiPhong where Ma_LoaiPhong = ('" + lp.Maloaiphong + "')";
            SqlCommand command = new SqlCommand(sql, connection);
            int x = command.ExecuteNonQuery();
            if (x <= 0)
                kq = false;
           DongKetNoi(connection);
            return kq;
        }
    }
}
